﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Lucene.Net;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Store;
using Lucene.Net.Util;

namespace OgilvyOne.UKNow.BLL
{
    public class LuceneBll
    {
        public void CreateFolderIndex(string lucenePath, IList<Document> documents,string cnen)
        {
            Directory dir = FSDirectory.Open(new System.IO.DirectoryInfo(lucenePath));
            IndexWriter iw = new IndexWriter(dir, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29), true, new KeepOnlyLastCommitDeletionPolicy(), IndexWriter.MaxFieldLength.LIMITED);
            foreach (Document doc in documents)
            {
                //Document doc = this.GetLuceneDoc(model,cnen);
                iw.AddDocument(doc);
            }
            iw.Optimize();
            iw.Close();
        }

        private Query GetQuery(string fieldvalue, string value, Analyzer analyzer)
        {
            QueryParser bshowParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, fieldvalue, analyzer);
            Query bshowQuery = bshowParser.Parse(value);
            return bshowQuery;
        }

        private Document GetLuceneDoc(Model.EventsModel model,string cnen)
        {
            Document doc = new Document();
            doc.Add(new Field("html", model.Detail, Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("title", model.Title, Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("url", "/" + cnen + "/events/" + model.ID.Value.ToString() + ".html", Field.Store.YES, Field.Index.NOT_ANALYZED));
            //doc.Add(new Field("ticketurl", model.TicketUrl, Field.Store.YES, Field.Index.NOT_ANALYZED));
            //doc.Add(new Field("id", model.ID.Value.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            //doc.Add(new Field("columnid", model.ColumnID.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            //doc.Add(new Field("listimg", model.ListImg, Field.Store.YES, Field.Index.NOT_ANALYZED));
            //doc.Add(new Field("shortdescription", model.ShortDescription, Field.Store.YES, Field.Index.NOT_ANALYZED));
            //doc.Add(new Field("subtitle", model.SubTitle, Field.Store.YES, Field.Index.NOT_ANALYZED));
            //doc.Add(new Field("topimg", model.TopImg, Field.Store.YES, Field.Index.NOT_ANALYZED));
            //doc.Add(new Field("venue", model.Venue, Field.Store.YES, Field.Index.NOT_ANALYZED));
            //doc.Add(new Field("venueimagepath", model.VenueImagePath, Field.Store.YES, Field.Index.NOT_ANALYZED));
            return doc;
        }

        public void CreateFileIndex(string lucenePath, string fileUrl, string title, string content)
        {
            Directory dir = FSDirectory.Open(new System.IO.DirectoryInfo(lucenePath));
            IndexWriter iw = new IndexWriter(dir, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29), false, new KeepOnlyLastCommitDeletionPolicy(), IndexWriter.MaxFieldLength.LIMITED);
            Document doc = new Document();
            doc.Add(new Field("html", content, Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("title", title, Field.Store.YES, Field.Index.ANALYZED));
            doc.Add(new Field("url", fileUrl, Field.Store.YES, Field.Index.NOT_ANALYZED));
            iw.AddDocument(doc);
            iw.Optimize();
            iw.Close();
        }

        //public List<string[]> SearchHtml(string lucenePath, string seaStr, int pagesize, ref int pageindex)
        //{
        //    IndexSearcher searcher = new IndexSearcher(FSDirectory.Open(new System.IO.DirectoryInfo(lucenePath)), true);
        //    Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
        //    Query query = this.GetQuery("html", seaStr, analyzer);
        //    TopDocs topDocs = searcher.Search(query, 1000000);
        //    if (pageindex < 1)
        //    {
        //        pageindex = 1;
        //    }
        //    int maxpage = topDocs.totalHits / pagesize + (topDocs.totalHits % pagesize > 0 ? 1 : 0);
        //    if (pageindex > maxpage)
        //    {
        //        if (maxpage > 0)
        //        {
        //            pageindex = maxpage;
        //        }
        //    }
        //    List<string[]> list = new List<string[]>();
        //    int uplimit = pageindex * pagesize > topDocs.totalHits ? topDocs.totalHits : pageindex * pagesize;
        //    for (int i = (pageindex - 1) * pagesize; i < uplimit; i++)
        //    {
        //        ScoreDoc scoredoc = topDocs.scoreDocs[i];
        //        Document doc = searcher.Doc(scoredoc.doc);
        //        string[] tempArr = new string[] { 
        //                                            doc.Get("html"), 
        //                                            doc.Get("title"), 
        //                                            doc.Get("ticketurl"),
        //                                            doc.Get("id"),
        //                                            doc.Get("columnid"),
        //                                            doc.Get("listimg"),
        //                                            doc.Get("shortdescription"),
        //                                            doc.Get("subtitle"),
        //                                            doc.Get("topimg"),
        //                                            doc.Get("venue"),
        //                                            doc.Get("venueimagepath")
        //                                        };
        //        list.Add(tempArr);
        //    }
        //    return list;
        //}

        public void DeleteLuceneFolder(string lucenePath)
        {
            Directory dir = FSDirectory.Open(new System.IO.DirectoryInfo(lucenePath));
            IndexWriter iw = new IndexWriter(dir, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29), true, new KeepOnlyLastCommitDeletionPolicy(), IndexWriter.MaxFieldLength.LIMITED);
            iw.DeleteAll();
            iw.Close();
        }
    }
}
